<?php
ini_set('date.timezone', 'Asia/Shanghai');
require_once("inc/config.php");
require_once("inc/function.php");
$act = $_REQUEST['act'] ? $_REQUEST['act'] : ''; //接收是那种操作
$cookie = "./cookie/cookie.txt";
logRes(json_encode($_REQUEST), "access"); //记录登录日志


//ip鉴权
if ($config['verification_ip']) {
    $remote_addr = $_SERVER['REMOTE_ADDR'];
    if (!in_array($remote_addr, $config['ip_list'])) {
        logRes($remote_addr . "请把该ip添加到配置文件！", "ip_deny"); //记录登录日志
        echo '{"status": false, "msg": "请先配置您的ip到白名单中。"} ';
        die;
    }
}

/*
//登录宝塔后台
$url = $config['login_url'];
$post_data = array();
$post_data['username'] = $config['username'];
$post_data['password'] = $config['password'];
$rt = login_bt($url, $post_data, $cookie);
echo $rt;
logRes($rt, "bt_login"); //记录登录日志
*/



//shell登录宝塔后台
$post_str="username=". $config['username']."&password=".$config['password'];
$url = $config['login_url'];
$rt = login_bt_shell($url, $post_str, $cookie);
logRes($rt, "bt_login_shell"); //记录登录日志


/*
//windows下的cmd命令登录宝塔后台
$post_str="username=". $config['username']."&password=".$config['password'];
$url = $config['login_url'];
$rt = login_bt_cmd($url, $post_str, $cookie);
logRes($rt, "bt_login_shell"); //记录登录日志
*/



//获取站点列表
if ($act == 'get_weblist') {
    $url = $config['get_weblist_url'];
    $post_data = array();
    $post_data['tojs'] = 'site.get_list';
    $post_data['table'] = 'sites';
    $post_data['limit'] = '100000';
    $post_data['p'] = '1';
    $post_data['search'] = '';
    $post_data['order'] = 'id desc';
    $post_data['type'] = '-1';
    $weblist = get_weblist($url, $post_data, $cookie);
    logRes($weblist, "get_weblist"); //记录获取站点列表日志
    echo $weblist;
    die;

}

//获取某个站点的域名列表
if ($act == 'get_webinfo') {
    $search = $_REQUEST['search'];
    $url = $config['get_webinfo_url'];
    $post_data = array();
    $post_data['list'] = true;
    $post_data['search'] = $search;
    $post_data['table'] = 'domain';
    $webinfo = get_webinfo($url, $post_data, $cookie);
    logRes($webinfo, "get_webinfo"); //记录获取某个站点的域名列表日志
    echo $webinfo;
    die;
}


//给某个站点添加域名
if ($act == 'add_domain') {
    //接收参数
    $id = $_REQUEST['id'];
    $webname = $_REQUEST['webname'];
    $domain = $_REQUEST['domain'];
    $url = $config['add_domain_url'];
    //构造数据
    $post_data = array();
    $post_data['id'] = $id;
    $post_data['webname'] = $webname;
    $post_data['domain'] = $domain;
    $rs = add_domain($url, $post_data, $cookie);
    logRes($rs, "add_domain"); //记录添加日志
    echo $rs;
    die;
}


//给某个站点删除域名
if ($act == 'del_domain') {
    //接收参数
    $id = $_REQUEST['id'];
    $webname = $_REQUEST['webname'];
    $domain = $_REQUEST['domain'];
    $port = $_REQUEST['port'];
    //构造数据
    $post_data = array();
    $post_data['id'] = $id;
    $post_data['webname'] = $webname;
    $post_data['domain'] = $domain;
    $post_data['port'] = $port;
    $url = $config['del_domain_url'];
    $rs = del_domain($url, $post_data, $cookie);
    logRes($rs, "del_domain"); //记录删除日志
    echo $rs;
    die;
}

echo '{"status": false, "msg": "没有找到对应的方法！"} ';
die;
die;